package com.codi.api.xxljob;

import com.codi.base.cache.CacheUtil;
import com.codi.bus.constant.CacheKeyConst;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.service.StaticSH300Service;
import com.codi.bus.core.util.JobUtil;
import com.codi.bus.core.util.SH300Util;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author lvgo
 * @version 1.0
 * @Description: 获取最新的深沪300数据
 * 0 0 1 * * ?
 * @date 18-6-29 下午5:01
 */
@Slf4j
@Component
@JobHandler("sH300LatestHandler")
public class SH300LatestHandler extends IJobHandler {
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private StaticSH300Service staticSH300Service;

    @Override
    public ReturnT<String> execute(String param) throws Exception {
        log.debug("execute SH300 Latest Job");
        String key = CacheUtil.getKey("SH300", "LATEST");
        BoundHashOperations<String, String, String> op = redisTemplate.boundHashOps(key);
        redisTemplate.delete(key);
        SH300Util.loadData(GlobalConstant.URL_SH300_LATEST, op);
        // 调用业务服务器接口
        staticSH300Service.addLatestData();
        // 1,3,6,12个月
        int[] months = {1, 3, 6, 12};
        for (int month : months) {
            String key1 = CacheUtil.getKey("SH300", "STATIC_MONTH", month);
            redisTemplate.delete(key1);
            staticSH300Service.getStatisticData(month, null);
        }
        // 最新一条记录
        String key2 = CacheUtil.getKey("SH300", "MAX_TRADING_DATE_RECORD");
        redisTemplate.delete(key2);
        staticSH300Service.getLatestRecord();
        JobUtil.updateJobExecuteTime(CacheKeyConst.SH300_LATEST);
        XxlJobLogger.log("SH300LatestHandler, success");
        return SUCCESS;
    }


}



